package com.asking.Controller;

import com.asking.domain.Category;
import com.asking.domain.User;
import com.asking.service.CategoryService;
import com.asking.vo.Result;
import com.asking.vo.ResultVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: 宋定鑫
 * @Date: 2018/10/17 18:07
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/add")
    public ResponseEntity<Result> add(Category category){
        String name="";
        try{
            String addname = categoryService.add(category);
            name=addname;
            if("分类新增成功".equals(addname)){
                return new ResponseEntity<>(new Result(200,name), HttpStatus.OK);
            }
            return new ResponseEntity<>(new Result(500,name),HttpStatus.INTERNAL_SERVER_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,name),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PutMapping
    public ResponseEntity<Result> updatecategory(Category category){
        String name="";
        try{
            String updatename=categoryService.updata(category);
            name=updatename;
            if("分类修改成功".equals(updatename)){
                return new ResponseEntity<>(new Result(204,name),HttpStatus.NO_CONTENT);
            }
            return new ResponseEntity<>(new Result(500,name),HttpStatus.INTERNAL_SERVER_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,name),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<Result> idcategory(@PathVariable("id")Integer id){
        try {
            Category category =categoryService.selectBycategory(id);
            return new ResponseEntity<>(new Result(200,"",category),HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500,""),HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    @GetMapping("/findAll")
    public ResponseEntity<Result> findAll(Integer page,Integer rows){
        try {
            ResultVo all = categoryService.findAll(page,rows);
            return new ResponseEntity<>(new Result(200,"",all),HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500,""),HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Result> deleteidcategory(@PathVariable("id")Integer id){
        try{
            categoryService.deleteidcategory(id);
            return new ResponseEntity<>(new Result(204,"删除成功"),HttpStatus.NO_CONTENT);
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,"删除失败"),HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
